
// 验证邮箱
function isMail(mail){
    var _reg = /^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*))@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2,5})$/;
    return _reg.test(mail.toLowerCase());
}

// 验证座机号码
function isPhone(phone){
    var _reg = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{2,4}))?$/;
    return _reg.test(phone);
}

// 验证公司名(中文和括号[中英状态])
function isCompany(company){
    //var _reg = /^[\u4e00-\u9fa5\(\)\uff08\uff09]{4,50}$/;// 中文 | 英文状态的括号 | 中文状态的括号
    var _reg = /.{4,50}$/;// 中文 | 英文状态的括号 | 中文状态的括号
    return _reg.test(company);
}

//检测手机号码
function isMobile(mobile){
    var _regMobile = /(^1[3,4,5,7,8]{1}[0-9]{9}$)/;
    return _regMobile.test($.trim(mobile))
}

// 信息提示
function hint(_this, _s, _m) {  //onFocus
    var _msg 	= _this.nextAll('label:first');
    var _name 	= _this.attr('name');

    if (_msg.length == 0) {
        _msg = _this.after('<label></label>');
        _msg.attr('for', _name);
    }

    _msg.attr('style', 'display: block;')

    if (_s == 0) {
        _msg.removeClass('onCorrect');
        _msg.addClass('onError');
        _msg.attr('id', _name+'-error');

    } else {
        _msg.removeClass('onError');
        _msg.addClass('onCorrect');
        _msg.attr('id', _name+'-valid');
    }
    _msg.html(_m);
}

/**
 * 信息提示
 * 显示不同显示样式
 * @param  object   _this       选定的对象
 * @param  string   _m          消息内容
 * @param  number   type        显示样式
 *                   0:正确提示,绿色打钩
 *                   1:重要提示,黄色感叹号
 *                   2:一般提示,灰色无图标
 *                   3:错误提示,红色打叉
 *                   4:正确提示,绿色打钩,无文字
 *
 * @return
 */
function messageShow(_this, _m, type) {  //onFocus
    var _msg    = _this.nextAll('label:first');
    var _name   = _this.attr('id');

    if (_msg.length == 0) {
        _msg   = _this.after('<label></label>');
        _msg   = _this.nextAll('label:first');
        _msg.attr('for', _name);
    }

    var thisclass = '';
    switch (type) {
        case 0:
            thisclass = 'onLoad';
            break;
        case 1:
            thisclass = 'onFocus';
            break;
        case 2:
            thisclass = 'onShow';
            break;
        case 3:
            thisclass = 'onError';
            break;
        case 4:
            thisclass = 'onCorrect';
            break;
        default:
            thisclass = 'onLoad';
    }

    _msg.attr('style', 'display: block;')
    _msg.addClass(thisclass);
    _msg.attr('id', _name+'-'+thisclass);
    _msg.html(_m);
}


function errorHide(_this) {  //onFocus
    var _msg = _this.nextAll('label:first');

    if (_msg.length == 0) {
        return true;
    }
    if (_msg.hasClass('onError')) {
        _msg.remove();
        return true;
    }
}

// 浮动窗口居中(包括改变窗口大小)
function winCenter(win){
    var ww = $(window).width(), wh = $(window).height();
    var _win = $(win), w = _win.width(), h = _win.height();
    _win.css({'top':(wh-h)/2, 'left':(ww-w)/2});
}

// 刷新验证码
function refreshCode(){
    var _this = $('.rand-code-img'), _src =_this.attr('src') + '?rand=' + Math.random();
    _this.attr('src', _src);
    _this.prev('.rand-code-field').val('').focus();
}

// 显示且居中弹窗位置
function centerPopup(_ob){
    var  _win = $(window), _st = $(document).scrollTop();
    _ob.css({'top':(((_win.height() - _ob.height()) / 2) - 0 + _st), 'left':((_win.width() - _ob.width()) / 2)}).show();
}

// 显示(打开)弹窗的遮罩层
function openMask(_is){
    if(_is){
        $('#u-mask').css({'width':'100%', 'height':$(document).height()}).show();
    }else{
        $('#u-mask').hide();
    }
}

/* 规定多少秒后的自动跳转
 * Param [时间标签对象(jQuery), URL]
 */
function time2go(_ot, _u){
    var _t = _ot.text();
    var _init = setInterval(function(){
        var _i = --_t;
        _ot.html(_i);
        if(_i <= 0){
            window.location.href = _u;
            clearInterval(_init);
        }
    }, 1000);
}

// 键盘ESC键关闭弹窗
$(document).keyup(function(e){
    var _code = e.keyCode;
    if(_code == 27){
        openMask(false);
        $('div.u-popup').not(':hidden').hide();
    }
});


var format = function (number, form) {
    var forms = form.split('.'), number = '' + number, numbers = number.split('.')
        , leftnumber = numbers[0].split('')
        , exec = function (lastMatch) {
        if (lastMatch == '0' || lastMatch == '#') {
            if (leftnumber.length) {
                return leftnumber.pop();
            } else if (lastMatch == '0') {
                return lastMatch;
            } else {
                return '';
            }
        } else {
            return lastMatch;
        }
    }, string

    string = forms[0].split('').reverse().join('').replace(/./g, exec).split('').reverse().join('');
    string = leftnumber.join('') + string;

    if (forms[1] && forms[1].length) {
        leftnumber = (numbers[1] && numbers[1].length) ? numbers[1].split('').reverse() : [];
        string += '.' + forms[1].replace(/./g, exec);
    }
    //return string.replace(//.$/, '');
};


function detectIE() {
    var ua = window.navigator.userAgent;

    var msie = ua.indexOf('MSIE ');
    if (msie > 0) {
        // IE 10 or older => return version number
        return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
    }

    var trident = ua.indexOf('Trident/');
    if (trident > 0) {
        // IE 11 => return version number
        var rv = ua.indexOf('rv:');
        return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
    }

    var edge = ua.indexOf('Edge/');
    if (edge > 0) {
        // IE 12 => return version number
        return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
    }

    // other browser
    return false;
}


//获取手机验证码
function getMobileCode(thisObj, mobile) {
    var limitTime = 60;

    if(thisObj.hasClass('is-doing')){ // 控制60秒之后再次可点击
        return false;
    }
    if(mobile == ''){
        messageShow(thisObj, '手机号码不能为空！', 3);
        return false;

    } else if(!isMobile(mobile)){
        messageShow(thisObj, '手机号码格式不正确！', 3);
        return false;
    }

    $.ajax({
        'url':'/safe/getmobilecode',
        'type':'POST',
        'data':{'mobile':mobile},
        'dataType':'json',
        'async':true,

        'success': function (resp) {
            state = resp.state;
            switch(state) {
                case 1:
                    thisObj.addClass('is-doing').html(limitTime + '秒后重新发送');
                    var initDo = setInterval(function(){
                        var i = --limitTime;
                        var text = i + '秒后可重新发送';
                        thisObj.html(text);
                        if(i <= 0){
                            clearInterval(initDo);
                            thisObj.html('免费获取验证码').removeClass('is-doing');
                        }
                    }, 1000);

                    break;
                default:
                    var msg = resp.message;
                    messageShow(thisObj, msg, 3);
            }
        }
    });
    return false;
}


/**
 * Created by tech-php on 2016/4/8.
 */
